package cn.onepiece.smili.util;


import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: haoting.wang
 * @Date: Created in 上午11:18 2017/5/27
 */
public class MysqlToJavaTypeMapping {

    private static Map<String, String> javaTypeMapping = new HashMap<>(16);
    private static Map<String, String> jdbcTypeMapping = new HashMap<>(16);

    static{
        javaTypeMapping.put("VARCHAR","String");
        javaTypeMapping.put("CHAR","String");
        javaTypeMapping.put("BLOB","Byte[]");
        javaTypeMapping.put("TEXT","String");
        javaTypeMapping.put("INTEGER","Integer");
        javaTypeMapping.put("TINYINT","Integer");
        javaTypeMapping.put("SMALLINT","Integer");
        javaTypeMapping.put("MEDIUMINT","Integer");
        javaTypeMapping.put("BIT","Boolean");
        javaTypeMapping.put("BIGINT","Long");
        javaTypeMapping.put("BIGINT UNSIGNED","Long");
        javaTypeMapping.put("FLOAT","Float");
        javaTypeMapping.put("DOUBLE","Double");
        javaTypeMapping.put("DECIMAL","BigDecimal");
        javaTypeMapping.put("BOOLEAN","Integer");
        javaTypeMapping.put("ID","Long");
        javaTypeMapping.put("DATE","Date");
        javaTypeMapping.put("TIME","Date");
        javaTypeMapping.put("DATETIME","Date");
        javaTypeMapping.put("TIMESTAMP","Date");
        javaTypeMapping.put("YEAR","Date");
        javaTypeMapping.put("INT","Integer");
        Collections.unmodifiableMap(javaTypeMapping);


        jdbcTypeMapping.put("BIGINT","BIGINT");
        jdbcTypeMapping.put("BIGINT UNSIGNED","BIGINT");
        jdbcTypeMapping.put("BLOB","BLOB");
        jdbcTypeMapping.put("CHAR","CHAR");
        jdbcTypeMapping.put("CLOB","CLOB");
        jdbcTypeMapping.put("DECIMAL","DECIMAL");
        jdbcTypeMapping.put("DOUBLE","DOUBLE");
        jdbcTypeMapping.put("FLOAT","FLOAT");
        jdbcTypeMapping.put("INTEGER","INTEGER");
        jdbcTypeMapping.put("TEXT","VARBINARY");
        jdbcTypeMapping.put("NUMERIC","NUMERIC");
        jdbcTypeMapping.put("DATETIME","TIMESTAMP");
        jdbcTypeMapping.put("TIMESTAMP","TIMESTAMP");
        jdbcTypeMapping.put("DATE","DATE");
        jdbcTypeMapping.put("VARCHAR","VARCHAR");
        jdbcTypeMapping.put("TINYINT","TINYINT");
        jdbcTypeMapping.put("SMALLINT","SMALLINT");
        jdbcTypeMapping.put("INT","INTEGER");
        Collections.unmodifiableMap(jdbcTypeMapping);
    }



    public static String getJavaType(String mysqlType){

        String javaType = javaTypeMapping.get(mysqlType);
        if(StringUtils.isBlank(javaType)){
            return "String";
        }

        return javaType;
    }

    public static String getJdbcType(String mysqlType){

        String jdbcType = jdbcTypeMapping.get(mysqlType);
        if(StringUtils.isBlank(jdbcType)){
            return "VARCHAR";
        }

        return jdbcType;
    }
}
